package com.demomap;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;


import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collection;

public class SPUtil {
    private static volatile SPUtil sp = null;
    private SharedPreferences preferences;
    private Editor editor;
    private String strBase64;
    private byte[] base64Bytes;

    private SPUtil(Context context) {
        preferences = context.getSharedPreferences("userinfo", Activity.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public static SPUtil getInstance() {
        if (sp == null) {
            synchronized (SPUtil.class) {
                if (sp == null) {
                    sp = new SPUtil(MyApp.getInstance());
                }
            }
        }
        return sp;
    }

    public void clear() {
        editor.clear();
        editor.commit();
    }

    public void saveToken(String token) {
        saveString(token, "token");
    }

    public String loadToken() {
        return loadString("token").toString();
    }

    public void saveCardBean(ArrayList<CardBean> datas) {
        String str = MyApp.getGson().toJson(datas, new TypeToken<ArrayList<CardBean>>() {
        }.getType());
        saveString(str, "card");
    }

    public ArrayList<CardBean> loadCardBean() {
        ArrayList<CardBean> list;
        String str = loadString("card");
        list = MyApp.getGson().fromJson(str, new TypeToken<ArrayList<CardBean>>() {
        }.getType());
        if (null == list) {
            return new ArrayList<CardBean>();
        }
        return list;
    }


    private boolean saveString(String str, String name) {
        return editor.putString(name, str).commit();
    }

    private String loadString(String name) {
        return preferences.getString(name, "");
    }


    public boolean saveObj(Object obj, String name) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        strBase64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
        editor.putString(name, strBase64);
        editor.commit();
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public Object loadObj(String name) {
        Object obj = null;
        strBase64 = preferences.getString(name, "");
        if (TextUtils.isEmpty(strBase64)) {
            return "";
        }
        base64Bytes = Base64.decode(strBase64.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(bais);
            obj = ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

}
